<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Application extends CI_Controller {

    public $render_data;

    function __construct() {
        parent::__construct();

        $this->load->library('template');

        $this->load->library('carabiner');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->model('application_model');
    }

    function index() {
        $this->template->set_template('application');
        $this->template->write_view('content', 'backend/application/application_form');
        $this->template->render();
    }

    function payment() {
        $this->config->load("facebook", TRUE);
        $config = $this->config->item('facebook');
        $this->load->library('Facebook', $config);

        $userId = $this->facebook->getUser();

        // If user is not yet authenticated, the id will be zero
        if ($userId == 0) {
            // Generate a login url
            $login_url = $this->facebook->getLoginUrl(array('scope' => 'email'));

            echo '<script langquage="javascript">window.location="' . $login_url . '";</script>';
            exit;
        } else {
            // Get user's data and print it
            $user = $this->facebook->api('/me');
            $userId = $user['id'];
            $application_id = $this->application_model->get_id_by_fb($userId);
            if(!$application_id){
                redirect('register');
            }
        }

        if ($_POST) {
            $this->load->model('payment_model');
            $this->load->library('form_validation');
            $this->form_validation->set_rules('pay_date', 'Pay Date', 'trim|required');
//            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[members.member_email]');

            if ($this->form_validation->run() === TRUE) {

                $create_data = array(
                    'pay_date' => $this->input->post('pay_date'),
                    'application_id' => $this->input->post('application_id'),
                    'pay_bank' => $this->input->post('pay_bank'),
                    'slip_code' => $this->input->post('slip_code')
                );

                $insert_id = $this->payment_model->create($create_data);
                if ($insert_id) {
                    $this->application_model->payin($this->input->post('application_id'));
                    redirect('update');
                }
            } else {
                echo validation_errors();
                exit;
            }
        } else {
            
            $application_id = $this->application_model->get_id_by_fb($userId);
            $form_data = $this->application_model->get($application_id);
            $form_data['form_url'] = base_url('payment');
            
            $menu_data['payment_status'] = $form_data{'payment_status'};

            $this->template->set_template('application',$menu_data);
            $this->template->write_view('content', 'frontend/payment_form', $form_data);
            $this->template->render();
            return TRUE;
        }
    }

    function create() {

        $this->config->load("facebook", TRUE);
        $config = $this->config->item('facebook');
        $this->load->library('Facebook', $config);

        $userId = $this->facebook->getUser();

        // If user is not yet authenticated, the id will be zero
        if ($userId == 0) {
            // Generate a login url
            $login_url = $this->facebook->getLoginUrl(array('scope' => 'email'));

            echo '<script langquage="javascript">window.location="' . $login_url . '";</script>';
            exit;
        } else {
            // Get user's data and print it
            $user = $this->facebook->api('/me');
            $userId = $user['id'];
        }

        $application_id = $this->application_model->get_id_by_fb($userId);

        if ($application_id) {
            redirect('update');
        }


        if ($_POST) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('cdt_rank', 'cdt_rank', 'trim');
            if ($this->form_validation->run() === TRUE) {

                $admission_test = '';
                if($this->input->post('admission_test')!=''){
                    $admission_test = implode('|', $this->input->post('admission_test'));
                }
                
                $sources= '';
                if($this->input->post('sources')!=''){
                    $sources = implode('|', $this->input->post('sources'));
                }
                
                $required_doc='';
                if($this->input->post('required_doc')!=''){
                    $required_doc = implode('|', $this->input->post('required_doc'));
                }
                $required_doc_staff='';
                if($this->input->post('required_doc_staff'!='')){
                    $required_doc_staff = implode('|', $this->input->post('required_doc_staff'));
                }

                
                

                $update_data = array(
                    'cdt_rank' => $this->input->post('cdt_rank'),
                    'did_rank' => $this->input->post('did_rank'),
                    'isc_rank' => $this->input->post('isc_rank'),
                    'name_en' => $this->input->post('name_en'),
                    'name_th' => $this->input->post('name_th'),
                    'birthday' => $this->input->post('birthday'),
                    'nationality' => $this->input->post('nationality'),
                    'gender' => $this->input->post('gender'),
                    'religion' => $this->input->post('religion'),
                    'citizenship' => $this->input->post('citizenship'),
                    'citizen_id' => $this->input->post('citizen_id'),
                    'citizenship_of' => $this->input->post('citizenship_of'),
                    'passport_no' => $this->input->post('passport_no'),
                    'permanent_address' => $this->input->post('permanent_address'),
                    'telephone' => $this->input->post('telephone'),
                    'mobilephone' => $this->input->post('mobilephone'),
                    'mailing_address' => $this->input->post('mailing_address'),
                    'telephone2' => $this->input->post('telephone2'),
                    'email' => $this->input->post('email'),
                    'father_name' => $this->input->post('father_name'),
                    'father_occupation' => $this->input->post('father_occupation'),
                    'father_company' => $this->input->post('father_company'),
                    'father_telephone' => $this->input->post('father_telephone'),
                    'father_email' => $this->input->post('father_email'),
                    'mother_name' => $this->input->post('mother_name'),
                    'mother_occupation' => $this->input->post('mother_occupation'),
                    'mother_company' => $this->input->post('mother_company'),
                    'mother_telephone' => $this->input->post('mother_telephone'),
                    'mother_email' => $this->input->post('mother_email'),
                    'guardian_name' => $this->input->post('guardian_name'),
                    'guardian_occupation' => $this->input->post('guardian_occupation'),
                    'guardian_company' => $this->input->post('guardian_company'),
                    'guardian_telephone' => $this->input->post('guardian_telephone'),
                    'guardian_email' => $this->input->post('guardian_email'),
                    'academic_level_1' => $this->input->post('academic_level_1'),
                    'academic_language_1' => $this->input->post('academic_language_1'),
                    'academic_institution_1' => $this->input->post('academic_institution_1'),
                    'academic_major_1' => $this->input->post('academic_major_1'),
                    'academic_gpa_1' => $this->input->post('academic_gpa_1'),
                    'academic_from_1' => $this->input->post('academic_from_1'),
                    'academic_to_1' => $this->input->post('academic_to_1'),
                    'academic_level_2' => $this->input->post('academic_level_2'),
                    'academic_language_2' => $this->input->post('academic_language_2'),
                    'academic_institution_2' => $this->input->post('academic_institution_2'),
                    'academic_major_2' => $this->input->post('academic_major_2'),
                    'academic_gpa_2' => $this->input->post('academic_gpa_2'),
                    'academic_from_2' => $this->input->post('academic_from_2'),
                    'academic_to_2' => $this->input->post('academic_to_2'),
                    'academic_level_3' => $this->input->post('academic_level_3'),
                    'academic_language_3' => $this->input->post('academic_language_3'),
                    'academic_institution_3' => $this->input->post('academic_institution_3'),
                    'academic_major_3' => $this->input->post('academic_major_3'),
                    'academic_gpa_3' => $this->input->post('academic_gpa_3'),
                    'academic_from_3' => $this->input->post('academic_from_3'),
                    'academic_to_3' => $this->input->post('academic_to_3'),
                    'awards' => $this->input->post('awards'),
                    'activities' => $this->input->post('activities'),
                    'gpa_opt_1' => $this->input->post('gpa_opt_1'),
                    'gpa_opt_1_gpa' => $this->input->post('gpa_opt_1_gpa'),
                    'gpa_opt_2' => $this->input->post('gpa_opt_2'),
                    'gpa_opt_2_gpa' => $this->input->post('gpa_opt_2_gpa'),
                    'gpa_opt_2_math' => $this->input->post('gpa_opt_2_math'),
                    'gpa_opt_2_phy' => $this->input->post('gpa_opt_2_phy'),
                    'is_standard_test' => $this->input->post('is_standard_test'),
                    'english_test' => $this->input->post('english_test'),
                    'english_test_base' => $this->input->post('english_test_base'),
                    'english_test_overall' => $this->input->post('english_test_overall'),
                    'english_test_writing' => $this->input->post('english_test_writing'),
                    'english_test_reading' => $this->input->post('english_test_reading'),
                    'english_test_speaking' => $this->input->post('english_test_speaking'),
                    'english_test_listening' => $this->input->post('english_test_listening'),
                    'english_test_date' => $this->input->post('english_test_date'),
                    'science_test' => $this->input->post('science_test'),
                    'science_test_overall' => $this->input->post('science_test_overall'),
                    'science_test_math' => $this->input->post('science_test_math'),
                    'science_test_phy' => $this->input->post('science_test_phy'),
                    'science_test_chem' => $this->input->post('science_test_chem'),
                    'science_test_bio' => $this->input->post('science_test_bio'),
                    'science_test_date' => $this->input->post('science_test_date'),
                    'admission_test' => $admission_test,
                    'admission_test_onet' => $this->input->post('admission_test_onet'),
                    'admission_test_onet_date' => $this->input->post('admission_test_onet_date'),
                    'admission_test_anet' => $this->input->post('admission_test_anet'),
                    'admission_test_anet_date' => $this->input->post('admission_test_anet_date'),
                    'admission_test_gat' => $this->input->post('admission_test_gat'),
                    'admission_test_gat_date' => $this->input->post('admission_test_gat_date'),
                    'admission_test_pati' => $this->input->post('admission_test_pati'),
                    'admission_test_pati_date' => $this->input->post('admission_test_pati_date'),
                    'admission_test_patii' => $this->input->post('admission_test_patii'),
                    'admission_test_patii_date' => $this->input->post('admission_test_patii_date'),
                    'sources' => $sources,
                    'sources_specify_1' => $this->input->post('sources_specify_1'),
                    'sources_specify_2' => $this->input->post('sources_specify_2'),
                    'sources_specify_3' => $this->input->post('sources_specify_3'),
                    'sources_specify_4' => $this->input->post('sources_specify_4'),
                    'sources_specify_5' => $this->input->post('sources_specify_5'),
                    'another_school_1' => $this->input->post('another_school_1'),
                    'another_school_2' => $this->input->post('another_school_2'),
                    'another_school_3' => $this->input->post('another_school_3'),
                    'another_school_4' => $this->input->post('another_school_4'),
                    'suggestions' => $this->input->post('suggestions'),
                    'required_doc' => $required_doc,
                    'required_doc_staff' => $required_doc_staff,
                    'payment_type' => $this->input->post('payment_type'),
                    'facebook_id' => $userId
                );

                $insert_id = $this->application_model->create($update_data);
                if ($insert_id) {
                   $this->session->set_flashdata('success_message', 'Your application is saved');
                   redirect('update');
                }
            } else {
                echo validation_errors();
                exit;
            }
        } else {
            $form_data['form_url'] = base_url('frontend/application/create');
            $form_data['is_frontend'] = TRUE;
            //$menu_data['payment_status'] = $form_data{'payment_status'};
            $this->template->set_template('application');
            $this->template->write_view('application_header', 'frontend/application_header');
            $this->template->write_view('content', 'backend/application/application_form', $form_data);
            $this->template->render();
            return TRUE;
        }
    }

    function edit() {

        $this->config->load("facebook", TRUE);
        $config = $this->config->item('facebook');
        $this->load->library('Facebook', $config);

        $userId = $this->facebook->getUser();

        // If user is not yet authenticated, the id will be zero
        if ($userId == 0) {
            // Generate a login url
            $login_url = $this->facebook->getLoginUrl(array('scope' => 'email'));

            echo '<script langquage="javascript">window.location="' . $login_url . '";</script>';
            exit;
        } else {
            // Get user's data and print it
            $user = $this->facebook->api('/me');
            $userId = $user['id'];
        }

        $application_id = $this->application_model->get_id_by_fb($userId);

        if (!$application_id) {
            redirect('register');
        }



        if ($_POST) {

            $this->load->library('form_validation');
            $this->form_validation->set_rules('cdt_rank', 'cdt_rank', 'trim');
            if ($this->form_validation->run() === TRUE) {

                $admission_test = '';
                if($this->input->post('admission_test')!=''){
                    $admission_test = implode('|', $this->input->post('admission_test'));
                }
                
                $sources= '';
                if($this->input->post('sources')!=''){
                    $sources = implode('|', $this->input->post('sources'));
                }
                
                $required_doc='';
                if($this->input->post('required_doc')!=''){
                    $required_doc = implode('|', $this->input->post('required_doc'));
                }
                $required_doc_staff='';
                if($this->input->post('required_doc_staff'!='')){
                    $required_doc_staff = implode('|', $this->input->post('required_doc_staff'));
                }

                $update_data = array(
                    'cdt_rank' => $this->input->post('cdt_rank'),
                    'did_rank' => $this->input->post('did_rank'),
                    'isc_rank' => $this->input->post('isc_rank'),
                    'name_en' => $this->input->post('name_en'),
                    'name_th' => $this->input->post('name_th'),
                    'birthday' => $this->input->post('birthday'),
                    'nationality' => $this->input->post('nationality'),
                    'gender' => $this->input->post('gender'),
                    'religion' => $this->input->post('religion'),
                    'citizenship' => $this->input->post('citizenship'),
                    'citizen_id' => $this->input->post('citizen_id'),
                    'citizenship_of' => $this->input->post('citizenship_of'),
                    'passport_no' => $this->input->post('passport_no'),
                    'permanent_address' => $this->input->post('permanent_address'),
                    'telephone' => $this->input->post('telephone'),
                    'mobilephone' => $this->input->post('mobilephone'),
                    'mailing_address' => $this->input->post('mailing_address'),
                    'telephone2' => $this->input->post('telephone2'),
                    'email' => $this->input->post('email'),
                    'father_name' => $this->input->post('father_name'),
                    'father_occupation' => $this->input->post('father_occupation'),
                    'father_company' => $this->input->post('father_company'),
                    'father_telephone' => $this->input->post('father_telephone'),
                    'father_email' => $this->input->post('father_email'),
                    'mother_name' => $this->input->post('mother_name'),
                    'mother_occupation' => $this->input->post('mother_occupation'),
                    'mother_company' => $this->input->post('mother_company'),
                    'mother_telephone' => $this->input->post('mother_telephone'),
                    'mother_email' => $this->input->post('mother_email'),
                    'guardian_name' => $this->input->post('guardian_name'),
                    'guardian_occupation' => $this->input->post('guardian_occupation'),
                    'guardian_company' => $this->input->post('guardian_company'),
                    'guardian_telephone' => $this->input->post('guardian_telephone'),
                    'guardian_email' => $this->input->post('guardian_email'),
                    'academic_level_1' => $this->input->post('academic_level_1'),
                    'academic_language_1' => $this->input->post('academic_language_1'),
                    'academic_institution_1' => $this->input->post('academic_institution_1'),
                    'academic_major_1' => $this->input->post('academic_major_1'),
                    'academic_gpa_1' => $this->input->post('academic_gpa_1'),
                    'academic_from_1' => $this->input->post('academic_from_1'),
                    'academic_to_1' => $this->input->post('academic_to_1'),
                    'academic_level_2' => $this->input->post('academic_level_2'),
                    'academic_language_2' => $this->input->post('academic_language_2'),
                    'academic_institution_2' => $this->input->post('academic_institution_2'),
                    'academic_major_2' => $this->input->post('academic_major_2'),
                    'academic_gpa_2' => $this->input->post('academic_gpa_2'),
                    'academic_from_2' => $this->input->post('academic_from_2'),
                    'academic_to_2' => $this->input->post('academic_to_2'),
                    'academic_level_3' => $this->input->post('academic_level_3'),
                    'academic_language_3' => $this->input->post('academic_language_3'),
                    'academic_institution_3' => $this->input->post('academic_institution_3'),
                    'academic_major_3' => $this->input->post('academic_major_3'),
                    'academic_gpa_3' => $this->input->post('academic_gpa_3'),
                    'academic_from_3' => $this->input->post('academic_from_3'),
                    'academic_to_3' => $this->input->post('academic_to_3'),
                    'awards' => $this->input->post('awards'),
                    'activities' => $this->input->post('activities'),
                    'gpa_opt_1' => $this->input->post('gpa_opt_1'),
                    'gpa_opt_1_gpa' => $this->input->post('gpa_opt_1_gpa'),
                    'gpa_opt_2' => $this->input->post('gpa_opt_2'),
                    'gpa_opt_2_gpa' => $this->input->post('gpa_opt_2_gpa'),
                    'gpa_opt_2_math' => $this->input->post('gpa_opt_2_math'),
                    'gpa_opt_2_phy' => $this->input->post('gpa_opt_2_phy'),
                    'is_standard_test' => $this->input->post('is_standard_test'),
                    'english_test' => $this->input->post('english_test'),
                    'english_test_base' => $this->input->post('english_test_base'),
                    'english_test_overall' => $this->input->post('english_test_overall'),
                    'english_test_writing' => $this->input->post('english_test_writing'),
                    'english_test_reading' => $this->input->post('english_test_reading'),
                    'english_test_speaking' => $this->input->post('english_test_speaking'),
                    'english_test_listening' => $this->input->post('english_test_listening'),
                    'english_test_date' => $this->input->post('english_test_date'),
                    'science_test' => $this->input->post('science_test'),
                    'science_test_overall' => $this->input->post('science_test_overall'),
                    'science_test_math' => $this->input->post('science_test_math'),
                    'science_test_phy' => $this->input->post('science_test_phy'),
                    'science_test_chem' => $this->input->post('science_test_chem'),
                    'science_test_bio' => $this->input->post('science_test_bio'),
                    'science_test_date' => $this->input->post('science_test_date'),
                    'admission_test' => $admission_test,
                    'admission_test_onet' => $this->input->post('admission_test_onet'),
                    'admission_test_onet_date' => $this->input->post('admission_test_onet_date'),
                    'admission_test_anet' => $this->input->post('admission_test_anet'),
                    'admission_test_anet_date' => $this->input->post('admission_test_anet_date'),
                    'admission_test_gat' => $this->input->post('admission_test_gat'),
                    'admission_test_gat_date' => $this->input->post('admission_test_gat_date'),
                    'admission_test_pati' => $this->input->post('admission_test_pati'),
                    'admission_test_pati_date' => $this->input->post('admission_test_pati_date'),
                    'admission_test_patii' => $this->input->post('admission_test_patii'),
                    'admission_test_patii_date' => $this->input->post('admission_test_patii_date'),
                    'sources' => $sources,
                    'sources_specify_1' => $this->input->post('sources_specify_1'),
                    'sources_specify_2' => $this->input->post('sources_specify_2'),
                    'sources_specify_3' => $this->input->post('sources_specify_3'),
                    'sources_specify_4' => $this->input->post('sources_specify_4'),
                    'sources_specify_5' => $this->input->post('sources_specify_5'),
                    'another_school_1' => $this->input->post('another_school_1'),
                    'another_school_2' => $this->input->post('another_school_2'),
                    'another_school_3' => $this->input->post('another_school_3'),
                    'another_school_4' => $this->input->post('another_school_4'),
                    'suggestions' => $this->input->post('suggestions'),
                    'required_doc' => $required_doc,
                    'required_doc_staff' => $required_doc_staff,
                    'payment_type' => $this->input->post('payment_type')
                );


                $this->application_model->edit($application_id, $update_data);
                $this->session->set_flashdata('success_message', 'Your application is updated');
                redirect('update');
            } else {
                echo validation_errors();
                exit;
            }
        } else {


            $form_data = $this->application_model->get($application_id);
            $form_data['form_url'] = base_url('frontend/application/edit/' . $application_id);
            $menu_data['payment_status'] = $form_data{'payment_status'};
            $form_data['is_frontend'] = TRUE;

            $this->template->set_template('application');

            $this->template->write_view('application_header', 'frontend/application_header', $menu_data);
            $this->template->write_view('content', 'backend/application/application_form', $form_data);
            $this->template->render();
            return TRUE;
        }
    }

    function test($application_id = 0) {
        $form_data = $this->application_model->get($application_id);
        $form_data['form_url'] = base_url('frontend/application/edit');

        $this->load->view('frontend/pdf', $form_data);
    }

    function pdf() {

        $this->config->load("facebook", TRUE);
        $config = $this->config->item('facebook');
        $this->load->library('Facebook', $config);

        $userId = $this->facebook->getUser();

        // If user is not yet authenticated, the id will be zero
        if ($userId == 0) {
            // Generate a login url
            $login_url = $this->facebook->getLoginUrl(array('scope' => 'email'));
            echo '<a href="' . $login_url . '">Click here to login</a>';
            exit;
        } else {
            // Get user's data and print it
            $user = $this->facebook->api('/me');
            $userId = $user['id'];
        }

        $application_id = $this->application_model->get_id_by_fb($userId);

        if (!$application_id) {
            redirect('frontend/application/create');
        }

        //$pdfFilePath = FCPATH . "/downloads/application_pdf/".$application_id.".pdf";
        //if (file_exists($pdfFilePath) == FALSE) {


        $form_data = $this->application_model->get($application_id);
        $form_data['form_url'] = base_url('frontend/application/edit');

        $pdf_name =  md5($application_id.'awedsf');
        $pdfFilePath = FCPATH . "/downloads/application_pdf/" .$pdf_name. ".pdf";


        $html = $this->load->view('frontend/pdf', $form_data, TRUE);

        ini_set('memory_limit', '32M');

        $this->load->library('pdf');
        $pdf = $this->pdf->load();
        //$pdf->SetFooter($_SERVER['HTTP_HOST'] . '|{PAGENO}|' . date(DATE_RFC822)); // Add a footer for good measure <img src="http://davidsimpson.me/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley"> 

        $pdf->WriteHTML($html); // write the HTML into the PDF
        $pdf->Output($pdfFilePath, 'F'); // save to file because we can
        //}

        redirect(base_url("/downloads/application_pdf/" . $pdf_name . ".pdf?r=".time()));
    }

}
